//
//  DRHttpTool.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/19.
//

#import "DRHttpTool.h"
#import <AFNetworking/AFNetworkActivityIndicatorManager.h>
#import "NSString+DR.h"
#import <YYModel/YYModel.h>

@interface DRHttpTool()
@property (nonatomic, readwrite)  AFHTTPSessionManager *manager;
@end

@implementation DRHttpTool
    
+(instancetype)shared{
    static DRHttpTool *global = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        // 打开状态栏的等待菊花
        [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
        global = [[self alloc] init];
    });
    return global;
}

-(void)POST:(NSString *)url andParams:(NSDictionary *)parameters andSucc:(void (^)(id))succBlock andFail:(void (^)(NSError * _Nonnull))failBlock{
    [self.manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DRResponseModel *data = [DRResponseModel yy_modelWithJSON:responseObject];
        if (data.isError) {
            if (failBlock) {
                NSString *domain = @"com.DR.ErrorDomain";
                NSString *desc = NSLocalizedString(data.msg ,nil);
                NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
                NSError *error = [NSError errorWithDomain:domain
                                                     code:-101
                                                 userInfo:userInfo];
                failBlock(error);
            }
        }else{
            if (data.code == 500) {
                if (failBlock) {
                    NSString *domain = @"com.DR.ErrorDomain";
                    NSString *desc = NSLocalizedString(data.datas[@"msg"] ,nil);
                    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
                    NSError *error = [NSError errorWithDomain:domain
                                                         code:500
                                                     userInfo:userInfo];
                    failBlock(error);
                }
            }else{
                if (succBlock) {
                    if (data.data) {
                        succBlock(data.data);
                    }else {
                        succBlock(responseObject);
                    }
                }
            }

        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

-(void)GET:(NSString *)url andParams:(NSDictionary *)parameters andSucc:(void (^)(id))succBlock andFail:(void (^)(NSError * _Nonnull))failBlock{
    
    [self.manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DRResponseModel *data = [DRResponseModel yy_modelWithJSON:responseObject];
        if (data.isError) {
            if (failBlock) {
                NSString *domain = @"com.DR.ErrorDomain";
                NSString *desc = NSLocalizedString(data.msg ,nil);
                NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
                NSError *error = [NSError errorWithDomain:domain
                                                     code:-101
                                                 userInfo:userInfo];
                failBlock(error);
            }
        }else{
            if (data.code == 500) {
                if (failBlock) {
                    NSString *domain = @"com.DR.ErrorDomain";
                    NSString *desc = NSLocalizedString(data.datas[@"msg"] ,nil);
                    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
                    NSError *error = [NSError errorWithDomain:domain
                                                         code:500
                                                     userInfo:userInfo];
                    failBlock(error);
                }
            }else{
                if (succBlock) {
                    if (data.data) {
                        succBlock(data.data);
                    }else {
                        succBlock(responseObject);
                    }
                }
            }if (succBlock) {
                if (data.data) {
                    succBlock(data.data);
                }else {
                    succBlock(responseObject);
                }
            }
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}


-(void)new_GET:(NSString *)url andParams:(NSDictionary *)parameters andSucc:(void (^)(id _Nonnull))succBlock andFail:(void (^)(NSError * _Nonnull))failBlock{
    [self.manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DRResponseModel *data = [DRResponseModel yy_modelWithJSON:responseObject];
        if (data.code == 200) {
            if (succBlock) {
                succBlock(data.datas);
            }
        }else{
            NSString *domain = @"com.DR.ErrorDomain";
            NSString *desc = NSLocalizedString(data.datas[@"msg"] ,nil);
            NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
            NSError *error = [NSError errorWithDomain:domain
                                                 code:data.code
                                             userInfo:userInfo];
            failBlock(error);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}


-(void)new_POST:(NSString *)url andParams:(NSDictionary *)parameters andSucc:(void (^)(id _Nonnull))succBlock andFail:(void (^)(NSError * _Nonnull))failBlock{
    [self.manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DRResponseModel *data = [DRResponseModel yy_modelWithJSON:responseObject];
        if (data.code == 200) {
            if (succBlock) {
                succBlock(data.datas);
            }
        }else{
            NSString *domain = @"com.DR.ErrorDomain";
            NSString *desc ;
            if ([data.datas[@"msg"] isKindOfClass:[NSNull class]]) {
                desc = @"请求失败!";
            }else{
               desc = NSLocalizedString(data.datas[@"msg"] ,nil);
            }
            NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };
            NSError *error = [NSError errorWithDomain:domain
                                                 code:data.code
                                             userInfo:userInfo];
            failBlock(error);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}


-(AFHTTPSessionManager *)manager{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.requestSerializer.timeoutInterval = 30.f;
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/plain", @"text/javascript", @"text/xml", @"image/*",@"Content-Type", nil];
    return  sessionManager;
}


@end

@interface DRResponseModel()<YYModel>

@end

@implementation DRResponseModel

+(NSDictionary<NSString *,id> *)modelCustomPropertyMapper{
    return @{@"isError" : @"error"};
}

@end
